package com.accounting.client.frontend.order.handlers;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.OrderStatues;
import com.accounting.client.remote.obj.Order;
import com.accounting.client.resource.BoxResources;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.delegate.Action;
import corelib.client.delegate.CompleteCallback;
import corelib.client.schedule.DeferredTask;
import corelib.client.ui.SimpleMask;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.messagebox.SimpleMessageBox;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;
import corelib.client.ui.widget.toolbar.SimpleRibbon;
import corelib.client.ui.widget.toolbar.SimpleRibbonButton;
import corelib.client.ui.widget.toolbar.SimpleRibbonButton.ButtonSize;

public class OrderPackageHandler extends SimpleDialogHandler {
	
	private Order  orderInfo;
	private SimpleRibbon toolBar;
	private FlowPanel contentPanel;
	
	private Action action;
	
	public OrderPackageHandler(Order packageInfo, Action action) {
		this.orderInfo = packageInfo;
		this.action = action;
	}
	
	@Override
	public String getHeading() {
		return "订单包裹查询";
	}

	@Override
	public Size getSize() {
		return new Size(750, 600);
	}

	@Override
	public IsButton[] getButtons() {
		return new IsButton[0];
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	@Override
	public Widget getContentPanel() {
		toolBar = createToolbar();
		contentPanel = new FlowPanel();
		SimpleLayoutPanel panel = new SimpleLayoutPanel();
		panel.add(toolBar);
		panel.add(contentPanel);
		contentPanel.getElement().getStyle().setPadding(10, Unit.PX);
		contentPanel.getElement().getStyle().setFontSize(13, Unit.PX);
		contentPanel.getElement().getStyle().setOverflowY(Overflow.SCROLL);
		
		panel.setWidgetTopHeight(toolBar, 0, Unit.PX, 80, Unit.PX);
		panel.setWidgetTopBottom(contentPanel, 80, Unit.PX, 0, Unit.PX);
		return panel;
	}
	
	private void track() {
		SimpleMask.show(contentPanel,"努力查询中...");
		String passport = SessionManager.getPassport();
		Services.OrderService.TrackOrder(passport, orderInfo.getId(), 
			new AsyncCallback<String>(){
				@Override
				public void onFailure(Throwable caught) {
					SimpleMask.hide(contentPanel);
					ErrorHandler.handleServiceError(caught);
				}
				
				@Override
				public void onSuccess(String result) {
					contentPanel.getElement().setInnerHTML(result);
					SimpleMask.hide(contentPanel);
				}
			}
		);
	}
	
	@Override
	public void onShowing() {
		super.onShowing();
		new DeferredTask().run(new ScheduledCommand(){
			@Override
			public void execute() {
				track();
			}
		}, 400);
	}

	protected SimpleRibbon createToolbar(){
		SimpleRibbon ribbon = new SimpleRibbon();
		ribbon.addPushButton(new SimpleRibbonButton("升级为完成", BoxResources.INSTANCE.shipping32(),
				ButtonSize.Medium,
				new WidgetListener(){
					@Override
					public void valueUpdated(Widget widget) {
						update();
					}
				}
	    ));
		ribbon.addSeparator(ButtonSize.Medium);
		ribbon.addPushButton(new SimpleRibbonButton("重新查询", BoxResources.INSTANCE.track32(),
				ButtonSize.Medium,
				new WidgetListener(){
					@Override
					public void valueUpdated(Widget widget) {
						track();
					}
				}
	    ));
		return ribbon;
	}
	
	private void update() {
		SimpleMask.mask(getDialog(), "Updating...");
		Services.OrderService.UpdateOrderStatus(SessionManager.getPassport(), orderInfo.getId(),OrderStatues.Complete,
			new AsyncCallback<Boolean>() {
				@Override
				public void onFailure(Throwable caught) {
					SimpleMask.unmask(getDialog());
					ErrorHandler.handleServiceError(caught);
				}
				
				@Override
				public void onSuccess(Boolean result) {
					SimpleMask.unmask(getDialog());
					if (result == Boolean.TRUE) {
						SimpleMessageBox.info("成功", "此订单已成功升级为完成", new CompleteCallback() {
							@Override
							public void onCompleted() {
								closeDialog();
								if (action != null){
									action.run();
								}
							}
						});
					}
				}
			
			}
		);
	}
	
}
